﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Xml;

namespace Prowess.OIS.Service.Setting
{
    public class OISServiceSetting
    {
        private ServiceHost host = null;
        private string urlMeta, urlService = "";

        public event EventHandler host_Opening;

        public event EventHandler host_Opened;

        public OISServiceSetting()
        {

        }
        public bool StartService()
        {
            try
            {
                //urlService = "net.tcp://" + GetLocalIPAddress() + ":4523/PantherRTPACSService";
                urlService = "net.tcp://127.0.0.1:4527/OISService";
                host = new ServiceHost(typeof(OISService));

                NetTcpBinding tcpBinding = new NetTcpBinding();
                tcpBinding.TransactionFlow = false;
                tcpBinding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
                tcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
                tcpBinding.Security.Mode = SecurityMode.None;
                tcpBinding.MaxReceivedMessageSize = 2147483647;
                tcpBinding.ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas();
                tcpBinding.ReaderQuotas.MaxArrayLength = 2147483647;
                tcpBinding.ReaderQuotas.MaxStringContentLength = 2147483647;
                tcpBinding.ReceiveTimeout = TimeSpan.FromMinutes(10);
                tcpBinding.SendTimeout = TimeSpan.FromMinutes(10);
                tcpBinding.OpenTimeout = TimeSpan.FromMinutes(10);
                tcpBinding.CloseTimeout = TimeSpan.FromMinutes(10);                

                host.AddServiceEndpoint(typeof(IOISService), tcpBinding, urlService);

                ServiceThrottlingBehavior stb = new ServiceThrottlingBehavior();
                stb.MaxConcurrentCalls = 1000;
                stb.MaxConcurrentSessions = 1000;
                stb.MaxConcurrentInstances = 1000;
                host.Description.Behaviors.Add(stb);

                host.Opening +=host_Opening;
                host.Opened += host_Opened;
                ((ServiceBehaviorAttribute)host.Description.Behaviors[typeof(ServiceBehaviorAttribute)]).MaxItemsInObjectGraph = 2147483647;
                ((ServiceBehaviorAttribute)host.Description.Behaviors[typeof(ServiceBehaviorAttribute)]).IncludeExceptionDetailInFaults = true;

                ((ServiceBehaviorAttribute)host.Description.Behaviors[typeof(ServiceBehaviorAttribute)]).ConcurrencyMode = ConcurrencyMode.Multiple;
                ((ServiceBehaviorAttribute)host.Description.Behaviors[typeof(ServiceBehaviorAttribute)]).InstanceContextMode = InstanceContextMode.PerCall;

                ServiceMetadataBehavior metadataBehavior;
                metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
                if (metadataBehavior == null)
                {
                    metadataBehavior = new ServiceMetadataBehavior();
                    //metadataBehavior.HttpGetUrl = new Uri("http://" + GetLocalIPAddress() + ":8001/PantherRTPACSService");
                    metadataBehavior.HttpGetUrl = new Uri("http://127.0.0.1:8007/OISService");
                    metadataBehavior.HttpGetEnabled = true;
                    metadataBehavior.ToString();
                    host.Description.Behaviors.Add(metadataBehavior);
                    urlMeta = metadataBehavior.HttpGetUrl.ToString();
                }

                host.Open();                
            }
            catch (Exception ae)
            {
                throw ae;
            }
            return true;
        }       
    }
}
